// pages/home/home.ts
import { Request, Groups } from '../../utils/util'
import Notify from '@vant/weapp/notify/notify';
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        navBarData: { // 导航栏的数据
            title: '个人中心',
            scan: false,
            navigateBack: false
        },
        userInfo: '',
        WxuserInfo: {},
        groups: Groups,
        group: 0,
        loading:true
    },
    updateInfo(e) {
        let field = e.currentTarget.dataset.field
        let value = e.detail.value
        if (field == 'group') {
            value = Groups[value]
        }
        if (this.data.userInfo[field] != value) {
            Request({
                url: '/updateuser',
                method: 'post',
                data: {
                    field: field,
                    value: value
                }
            }).then(res => {
                if (res.success) {
                    Notify({
                        color: '#ffffff',
                        background: app.globalData.notifyColor,
                        message: res.msg,
                        top: app.globalData.navBarHeight
                    });
                }
            }).catch(error => {
                console.log(error);
            });
        }
    },
    logout() {
        Request({
            url: '/logout',
            method: 'post',
        }).then(res => {
            console.log(res)
            wx.setStorageSync('token', 'a.b.c');
            Notify({
                color: '#ffffff',
                background: app.globalData.notifyColor,
                message: '已登出，请稍等',
                top: app.globalData.navBarHeight
            });
            setTimeout(function () {
                wx.reLaunch({
                    url: '/pages/home/home'
                })
            }, 2000)
        }).catch(error => {
            console.log(error);
        });
    },
    showAvatar(){
        wx.navigateTo({
            url: `/pages/avatar/viewAvatar/viewAvatar?avatar=${this.data.WxuserInfo.avatarUrl}`
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        Request({
            url: '/getuser',
            method: 'get'
        }).then(res => {
            let avatar = `https://ui-avatars.com/api/?name=${res.name[0]}&background=F7608B&color=ffffff&rounded=true`
            if(res.avatar){
                avatar = res.avatar
            }
            this.setData({
                userInfo: res,
                group: Groups.indexOf(res.group),
                WxuserInfo: {
                    avatarUrl: avatar
                }
            })
        }).catch(error => {
            console.log(error);
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady(){
        this.setData({loading:false})
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})